package cn.aylog.simpleStack;

/**
 * 数据结构与算法 之
 *  栈
 */
public class Main {
    public static void main(String[] args) {
        SimpleStack ss = new SimpleStack(5);
        ss.push(1);
        ss.push(2);
        ss.push(3);
        ss.push(4);
//        ss.list();
        System.out.println(ss.pop());
    }
}

class SimpleStack {
    private Object data[];
    private int maxSize;
    private int index = -1;

    public SimpleStack(int maxSize) {
        this.maxSize = maxSize;
        data = new Object[maxSize];
    }

    public boolean isEmpty() {
        return index == -1;
    }

    public boolean isFull() {
        return index == maxSize - 1;
    }

    public void push(Object o) {
        if(isFull()) {
            System.out.println("栈已满~");
            return;
        }
        index++;
        data[index] = o;
    }

    public Object pop() {
        if(isEmpty()) {
            System.out.println("栈为空~");
            return null;
        }
        return data[index--];
    }

    public void list() {
        for(int i=data.length-1; i>0; i--) {
            System.out.printf("data[%d]=%d\n", i, data[i]);
        }
    }
}
